<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Unit Controller
 *
 * @package     VisualQ Local System
 * @author      CSU OJT Students '10 - '11 (Michael Roland B. Martin)
 * @copyright   Copyright (c) 2010 - 2011, Bitstop Network Services, Inc.
 * @version     1.0 January 27, 2011
 */

class Unit extends Controller {

    function Unit()
    {
        parent::Controller();

        $this->lang->load('local', 'English');
    }

    function index()
    {
        redirect(base_url() . 'unit/unit_function/');
    }

    function front_desk()
    {
        // Check if unit is registered
        if($this->session->userdata('unit_name'))
        {
            // Get settings
            $query = $this->db->get('settings');
            if($query->num_rows > 0)
            {
                foreach($query->result_array() as $row)
                {
                    $data['settings'][$row['name']] = $row['value'];
                }
            }

            // Get unit type and check if the unit is registered as front desk
            $type = $this->session->userdata('unit_type');
            if( ! strcmp($type, 'Front Desk') == 0)
            {
                if(strcmp($type, 'Display') == 0)
                {
                    redirect(base_url() . 'unit/display');
                }
                redirect(base_url() . 'login/');
            }

            $this->load->model('service_model');
            $query = $this->service_model->get_available_services();

            $data['services'] = $query->result_array();

            // Initialize services and closing codes tallies
            $this->load->model('statistics_model');
            $this->statistics_model->initialize_stats();

            if(strcmp($data['settings']['logo'], '') == 0)
            {
                $data['settings']['logo'] = 'png';
            }

            $this->load->view('front_desk_view', $data);
        }
        else
        {
            redirect(base_url() . 'login/');
        }
    }

    function display()
    {
        // Check if unit is registered
        if($this->session->userdata('unit_name'))
        {
            // Get unit type and check if unit is registered as display
            $type = $this->session->userdata('unit_type');
            if( ! strcmp($type, 'Display') == 0)
            {
                if(strcmp($type, 'Front Desk') == 0)
                {
                    redirect(base_url() . 'unit/front_desk');
                }
                redirect(base_url() . 'login/');
            }

            $this->load->model('service_model');
            $query = $this->service_model->get_available_services();
            $data['services'] = $query->result_array();

            $this->load->view('display_view', $data);
        }
        else
        {
            redirect(base_url() . 'login/');
        }
    }

    function unit_function()
    {
        $this->load->helper('cookie');

        // Check if unit is registered
        if($this->session->userdata('unit_type'))
        {
            // Check the unit type
            $type = $this->session->userdata('unit_type');
            if(strcmp($type, 'Front Desk') == 0)
            {
                // Set the front desk or display cookie
                $cookie = array(
                    'name'      => 'unit',
                    'value'     => 1,
                    'expire'    => 0
                );
                set_cookie($cookie);

                // Logout the user
                $data = array(
                    'id'            => '',
                    'account_type'  => ''
                );
                $this->session->unset_userdata($data);

                redirect(base_url() . 'unit/front_desk/');
            }
            elseif(strcmp($type, 'Display') == 0)
            {
                // Set the front desk or display cookie
                $cookie = array(
                    'name'      => 'unit',
                    'value'     => 1,
                    'expire'    => 0
                );
                set_cookie($cookie);

                // Logout the user
                $data = array(
                    'id'            => '',
                    'account_type'  => '',
                    'mode'          => ''
                );
                $this->session->unset_userdata($data);

                redirect(base_url() . 'unit/display/');
            }
        }
        redirect(base_url() . 'manager/unit/');
    }

}

/* End of file unit.php */
/* Location: ./system/application/controllers/unit.php */